import Vue from 'vue'
import Router from 'vue-router'

// 路由懒加载
const Login = () => import(/* webpackChunkName:"login_home_welcome" */ './components/login.vue')
const Home = () => import(/* webpackChunkName:"login_home_welcome" */ './components/home.vue')
const Welcome = () => import(/* webpackChunkName:"login_home_welcome" */ './components/welcome.vue')
// import Login from './components/login.vue'
// import Home from './components/home.vue'
// import Welcome from './components/welcome.vue'

const Users = () => import(/* webpackChunkName:"user" */ './components/user/users.vue')
// import Users from './components/user/users.vue'

const Rights = () => import(/* webpackChunkName:"power" */ './components/power/rights.vue')
const Roles = () => import(/* webpackChunkName:"power" */ './components/power/roles.vue')
// import Rights from './components/power/rights.vue'
// import Roles from './components/power/roles.vue'

const Cate = () => import(/* webpackChunkName:"goods" */ './components/goods/cate.vue')
const Params = () => import(/* webpackChunkName:"goods" */ './components/goods/params.vue')
const GoodsList = () => import(/* webpackChunkName:"goods" */ './components/goods/list.vue')
const AddGoods = () => import(/* webpackChunkName:"goods" */ './components/goods/addGoods.vue')
// import Cate from './components/goods/cate.vue'
// import Params from './components/goods/params.vue'
// import GoodsList from './components/goods/list.vue'
// import AddGoods from './components/goods/addGoods.vue'

const Order = () => import(/* webpackChunkName:"order" */ './components/order/order.vue')
// import Order from './components/order/order.vue'

const Report = () => import(/* webpackChunkName:"report" */ './components/report/report.vue')
// import Report from './components/report/report.vue'


Vue.use(Router)

// 解决重复点击导航栏子菜单报错问题，开发调试阶段解开，在项目上线时注释
// const originalPush = Router.prototype.push;
// Router.prototype.push = function push(location) {
//   return originalPush.call(this, location).catch(err => err)
// };

const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: Users },
        { path: '/rights', component: Rights },
        { path: '/roles', component: Roles },
        { path: '/categories', component: Cate },
        { path: '/params', component: Params },
        { path: '/goods', component: GoodsList },
        { path: '/goods/addGoods', component: AddGoods },
        { path: '/orders', component: Order },
        { path: '/reports', component: Report },
      ]
    }
  ]
});
// 路由导航守卫
// to 即将进入的路径
// from 当前准备离开的路径
// next 必须要调用的函数，放行 next() 或强制跳转 next('/强制跳转路径')
router.beforeEach((to, from, next) => {
  if (to.path == '/login') {
    return next();
  };
  const tokenStr = window.sessionStorage.getItem('token');
  if (!tokenStr) {
    return next('/login');
  };
  next()
});

export default router